﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using GroundWater.Data;
using GroundWater.BusinessActions;

using MonitoringTable = GroundWater.Data.MonitoringDataSet.MonitoringDataTable;
using MonitoringRow = GroundWater.Data.MonitoringDataSet.MonitoringRow;


namespace GroundWater.waterlevel
{
    public partial class CellVoltage : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "电池电压报表");
            if (!this.IsPostBack)
            {
                txtStartDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                txtEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                upAll.Visible = false;
                upToday.Visible = true;

                //BoundToday();
            }
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs args)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gridView.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (args.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        args.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortasc" : " sortdesc");
                    }
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            //string wellCode = drpWell.SelectedValue;
            //string startDate = txtStartDate.Value;
            //string endDate = txtEndDate.Value;

            //updPanel.Update();
            gvMonitoring.PageIndex = 0;

            upToday.Visible = false;
            upAll.Visible = true;
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            BoundToday();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //MonitoringTable dt = Monitoring.FetchByDatePaged(txtStartDate.Value, txtEndDate.Value, "", 1000000, 0);
            MonitoringTable dt = null;
            if (upToday.Visible)
            {
                dt = Monitoring.FetchWaterLeveLast(DateTime.Now.ToString("yyyy-MM-dd"));
            }
            else
            {
                dt = Monitoring.FetchByDatePaged(txtStartDate.Value, txtEndDate.Value, "", 1000000, 0);
            }
            //ExportToExcel("aa.xls", dt, gvMonitoring);
            string file = upToday.Visible ? DateTime.Now.ToString("yyyy-MM-dd") + "电池电压信息.xls" : txtStartDate.Value + "到" + txtEndDate.Value + "电池电压信息.xls";
            ExportToExcel(file, dt, gvMonitoring);
        }



        public void ExportToExcel(string fileName, DataTable dt, GridView gv)
        {
            this.Page.Response.Clear();
            this.Page.Response.Buffer = true;
            this.Page.Response.AddHeader("Content-type", "application/x-msdownload");
            this.Page.Response.ContentType = "application/vnd.ms-excel";
            this.Page.Response.Charset = "utf-8";
            this.Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\"");
            this.EnableViewState = false;

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            GridView grid = new GridView();

            grid.RowDataBound += new GridViewRowEventHandler(gvExport_RowDataBound);
            grid.DataSource = dt;
            grid.AutoGenerateColumns = false;
            grid.Columns.Clear();
            foreach (DataControlField col in gv.Columns)
            {
                if (col is ButtonField || col is CommandField)
                {
                    continue;
                }
                if (col is BoundField || col is TemplateField || col is CheckBoxField)
                {
                    grid.Columns.Add(col);
                }
                if (col is HyperLinkField)
                {
                    BoundField field = new BoundField();
                    field.DataField = ((HyperLinkField)col).DataTextField;
                    field.HeaderText = col.HeaderText;
                    grid.Columns.Add(field);
                }
            }

            grid.DataBind();
            grid.RenderControl(htw);

            this.Page.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            this.Page.Response.Write(@"<style> .text { mso-number-format:\@; } </style> "); ;
            //string fileName = HttpUtility.UrlEncode(ExcelFileName + ".xls", Encoding.GetEncoding("GB2312"));
            //HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename=" + fileName);
            this.Page.Response.Write(sw.ToString());
            this.Page.Response.End();
        }

        protected void gvExport_RowDataBound(object sender, GridViewRowEventArgs args)
        {
            if (args.Row.RowType == DataControlRowType.DataRow)
            {
                args.Row.Cells[4].Attributes.Add("class", "text");
            }
        }


        private void BoundToday()
        {
            upToday.Visible = true;
            upAll.Visible = false;
            gvToday.DataSourceID = null;
            gvToday.DataSource = Monitoring.FetchWaterLeveLast(DateTime.Now.ToString("yyyy-MM-dd"));
            gvToday.DataBind();
        }
    }
}
